package com.software.graduationproject.commons.config;


import cn.hutool.core.util.StringUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;

import com.baomidou.mybatisplus.core.injector.ISqlInjector;


import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.web.context.request.RequestContextListener;

import javax.sql.DataSource;
import java.util.Date;

@Configuration
public class MybatisConfig {
    /**
     * mybatis-plus SQL执行效率插件【生产环境可以关闭】
     */
    @Bean
    @ConditionalOnProperty(prefix = "mybatis-plus", name = "performance", havingValue = "true")
    public PerformanceInterceptor performanceInterceptor() {
        return new PerformanceInterceptor();
    }
    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }


    @Bean
    public RequestContextListener requestContextListener() {
        return new RequestContextListener();
    }
    @Bean
    @ConditionalOnMissingBean
    public MetaObjectHandler metaObjectHandler() {
        return new MetaObjectHandler() {
            public void insertFill(MetaObject metaObject) {
                String username = "zyf";
                Date datenow = new Date();
                if (metaObject.hasSetter(StringUtil.toCamelCase("created_by"))) {
                    this.setFieldValByName(StringUtil.toCamelCase("created_by"), username, metaObject);
                }
                if (metaObject.hasSetter(StringUtil.toCamelCase("created_date"))) {
                    this.setFieldValByName(StringUtil.toCamelCase("created_date"), datenow, metaObject);
                }
                if (metaObject.hasSetter(StringUtil.toCamelCase("updated_by"))) {
                    this.setFieldValByName(StringUtil.toCamelCase("updated_by"), username, metaObject);
                }
                if (metaObject.hasSetter(StringUtil.toCamelCase("updated_date"))) {
                    this.setFieldValByName(StringUtil.toCamelCase("updated_date"), datenow, metaObject);
                }
            }
            public void updateFill(MetaObject metaObject) {
                String username = "zyf";
                Date datenow = new Date();
                if (metaObject.hasSetter(StringUtil.toCamelCase("updated_by"))) {
                    this.setFieldValByName(StringUtil.toCamelCase("updated_by"), username, metaObject);
                }
                if (metaObject.hasSetter(StringUtil.toCamelCase("updated_date"))) {
                    this.setFieldValByName(StringUtil.toCamelCase("updated_date"), datenow, metaObject);
                }
            }


        };
    }
}


